package com.utils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.util.UUIDUtil;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
/**
 * 
 * @author clj
 *
 */
public class HttpSmsClient {
	@Autowired
	private static HttpServletRequest request;
	
	private static final Logger logger = Logger.getLogger(HttpSmsClient.class);
	
	//短信时间
	public static String SMSTIME="60";
	
	//短信API
	public static String SMS_API_URL = "http://www.lx198.com/sdk/send";
	
	//注册短信模板
	public static String smsRegiterTokenTemplate= "尊敬的用户，您的验证码为" + createRandomVcode() + "，有效期为"+SMSTIME+"秒【寰微理财】";
	
	//登录短信模板
	public static String smsLoginTokenTemplate="";
	
	//修改手机号
	public static String smsgetUpdateMobileTokenTemplate="";

	 /**
     * @Discription:随机生成6位随机验证码
     * @return String
     * @Author: clj
     * @Date: 2018年5月21日14:40:09
    */
   public static String createRandomVcode(){
       //验证码
       String vcode = "";
       for (int i = 0; i < 6; i++) {
           vcode = vcode + (int)(Math.random() * 9);
       }
       HttpSession session = request.getSession();
       session.setAttribute("smscode", vcode);
       return vcode;
   }
   
   /**
    * @Discription:发送短信消息
    * @param phones
    * @param content
    * @return String
    * @Author: clj
    * @Date: 2018年5月21日14:41:16
   */
   public static String sendSms(String phones,String content){
      //短信接口URL提交地址
      Map<String, String> params = new HashMap<String, String>();
      params.put("accName", "13478651471");
      params.put("accPwd", "3E0158681807D0329396FACAAE45D96D");
      params.put("POST", "UTF-8");
      //手机号码，多个号码使用英文逗号进行分割
      params.put("aimcodes", phones);
      //将短信内容进行URLEncoder编码
      params.put("content", URLEncoder.encode(content));
      
      //发送短信成功 插入数据库
//      smsId = UUIDUtil.getUUID();
//      InsSmsLogBean insSmsLog = new InsSmsLogBean();
//      insSmsLog.setSmsId(smsId);//主键
//      insSmsLog.setUserId(null);//用户id
//      insSmsLog.setContent(content);//短信内容
//      insSmsLog.setCreateDate(new Date());//创建短信时间
//      insSmsLog.setOrgId(null);//机构id
//      insSmsLogMapper.insertInsSmsLog(insSmsLog);

      	return getRequest(SMS_API_URL, params);
   	}
   
	  /**
	   * @Discription:HttpClient 模拟GET请求
	   * @param url
	   * @param params
	   * @return String
	   * @Author: clj
	   * @Date: 2018年5月21日15:21:08
	  */
    public static String getRequest(String url, Map<String, String> params) {
	     //构造HttpClient实例
	     HttpClient client = new HttpClient();
	     //拼接参数
	     String paramStr = "";
	     for (String key : params.keySet()) {
	         paramStr = paramStr + "&" + key + "=" + params.get(key);
	     }
	     paramStr = paramStr.substring(1);
	     //创建GET方法的实例
	     GetMethod method = new GetMethod(url + "?" + paramStr);
	     //接收返回结果
	     String result = null;
	     try {
	         //执行HTTP GET方法请求
	         client.executeMethod(method);
	         //返回处理结果
	         result = method.getResponseBodyAsString();
	     }catch(HttpException e){
	         // 发生致命的异常，可能是协议不对或者返回的内容有问题
	         System.out.println("请检查输入的URL!");
	         e.printStackTrace();
	     }catch(IOException e) {
	         // 发生网络异常
	         System.out.println("发生网络异常!");
	         e.printStackTrace();
	     }finally{
	         //释放链接
	         method.releaseConnection();
	         //关闭HttpClient实例
	         if (client != null) {
	             ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
	             client = null;
	         }
	     }
	     return result;
	 }
}
